# coding=utf-8
# -*- coding: utf-8 -*-

import threading
from urllib import request
from bs4 import BeautifulSoup
import ssl
from douban.proxy import proxy

ssl._create_default_https_context = ssl._create_unverified_context

hdr = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
    'Accept-Encoding': 'none',
    'Accept-Language': 'en-US,en;q=0.8',
    'Connection': 'keep-alive'}


class detail(threading.Thread):  # The timer class is derived from the class threading.Thread
    def __init__(self, urls):
        threading.Thread.__init__(self)
        self.url = urls
        self.data = {}

    def run(self):  # Overwrite run() method, put what you want the thread do here
        print(self.url)
        opener = proxy()
        request.install_opener(opener.get_opener())
        req = request.Request(self.url, headers=hdr)
        f = request.urlopen(req)
        content = f.read().decode('utf-8')
        self.data = self.parse(content)
        print(self.data)

    def get_detail(self):
        try:
            if self.data:
                return self.data
            return None
        except Exception:
            return None

    def parse(self, content):
        result = {}
        html = BeautifulSoup(content, 'html5lib')
        info = html.find(id="info")
        # print(info)
        # print(html.find(property="v:itemreviewed").string)
        result['title'] = html.find(property="v:itemreviewed").string
        if info.find(rel="v:directedBy"):
            # print("导演：%s" % info.find(rel="v:directedBy").string)
            result['directedBy'] = info.find(rel="v:directedBy").string

        if info.find(text='编剧') and info.find(text='编剧').find_next('span', class_='attrs'):
            # print("编剧：%s" % info.find(text='编剧').find_next('span', class_='attrs').get_text())
            result['writer'] = info.find(text='编剧').find_next('span', class_='attrs').get_text().split("/")

        if info.find('span', class_='actor'):
            # print("演员：%s" % info.find('span', class_='actor').find('span', class_='attrs').get_text())
            result['actor'] = info.find('span', class_='actor').find('span', class_='attrs').get_text().split("/")

        if info.find('span', text='语言:'):
            # print("语言：%s" % info.find('span', text='语言:').next_sibling)
            result['langure'] = info.find('span', text='语言:').next_sibling

        if info.find('span', text='制片国家/地区:'):
            # print("制片国家/地区:：%s" % info.find('span', text='制片国家/地区:').next_sibling)
            result['country'] = info.find('span', text='制片国家/地区:').next_sibling

        if info.find_all(property="v:genre"):
            types = []
            # print("类型：%s" % info.find_all(property="v:genre"))
            for typeitem in info.find_all(property="v:genre"):
                # print(typeitem.get_text())
                types.append(typeitem.get_text())
            # print(types)
            result['genre'] = types

        if info.find(property="v:initialReleaseDate"):
            # print("上映日期：%s" % info.find(property="v:initialReleaseDate").string)
            result['open_date'] = info.find(property="v:initialReleaseDate").string

        if info.find(property="v:runtime"):
            # print("电影时长：%s" % info.find(property="v:runtime").string)
            result['long_time'] = info.find(property="v:runtime").string

        rate = html.find(id="interest_sectl")
        commend = {}

        if rate.find(property="v:average"):
            # print("该电影的评分%s" % rate.find(property="v:average").get_text())
            commend['average'] = rate.find(property="v:average").get_text()

        if rate.find(property="v:votes"):
            # print("%s 人评价了该电影" % rate.find(property="v:votes").get_text())
            commend['number'] = rate.find(property="v:votes").get_text()

        if rate.find(class_="stars5 starstop"):
            # print("5星好评 %s" % rate.find(class_="stars5 starstop").find_next("span").get_text())
            commend['5'] = rate.find(class_="stars5 starstop").find_next("span").get_text()

        if rate.find(class_="stars4 starstop"):
            # print("4星评 %s" % rate.find(class_="stars4 starstop").find_next("span").get_text())
            commend['4'] = rate.find(class_="stars4 starstop").find_next("span").get_text()

        if rate.find(class_="stars3 starstop"):
            # print("3星评 %s" % rate.find(class_="stars3 starstop").find_next("span").get_text())
            commend['3'] = rate.find(class_="stars3 starstop").find_next("span").get_text()

        if rate.find(class_="stars2 starstop"):
            # print("2星评 %s" % rate.find(class_="stars2 starstop").find_next("span").get_text())
            commend['2'] = rate.find(class_="stars2 starstop").find_next("span").get_text()

        if rate.find(class_="stars1 starstop"):
            # print("1星差评 %s" % rate.find(class_="stars1 starstop").find_next("span").get_text())
            commend['1'] = rate.find(class_="stars1 starstop").find_next("span").get_text()

        result['commend'] = commend

        return result
